package com.dp.service.impl3;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dp.pojo.ClientUpgradePojo;
import com.dp.pojo.JsonPage;
import com.dp.util.DataMap;
import com.dp.vo.NewsClientsoftupdaterule;

@Service
@Transactional
public class ClientUpgradeServiceImpl3 extends BaseImpl {
	
	public List<ClientUpgradePojo> getClientUpgrade(DataMap hashMap,ClientUpgradePojo ncu) {
		JsonPage jpage;
		Integer firstrow;
		Integer maxrow;
		String hql = "SELECT ncu FROM NewsClientsoftupdaterule ncu where 1=1 ";
		if (ncu !=null){
			if(ncu.getLastsn()!=null){
				hql += " and ncu.lastsn like '%" + ncu.getLastsn() + "%'";
			}
			if (ncu.getClientua() != null && !"".equals(ncu.getClientua())) {
				hql += " and ncu.newsClientsoftupdaterulePK.clientua like '%" + ncu.getClientua() + "%'";
			}
			if (ncu.getClientchannel() != null && !"".equals(ncu.getClientchannel())) {
				hql += " and ncu.newsClientsoftupdaterulePK.clientchannel like '%" + ncu.getClientchannel() + "%'";
			}
			if (ncu.getClientsn() != null && !"".equals(ncu.getClientsn())) {
				hql += " and ncu.newsClientsoftupdaterulePK.clientsn like '%" + ncu.getClientsn() + "%'";
			}
		}
		jpage = (JsonPage) hashMap.get("jpage");
		if(jpage!=null){
			if(jpage.getSidx()!=null&&!"".equals(jpage.getSidx())){
				hql += " order by "+jpage.getSidx();
				hql+=" "+jpage.getSord();
			}
		}
		
		List<ClientUpgradePojo> list=new ArrayList<ClientUpgradePojo>();
		try {
			Query query = em.createQuery(hql.toString());
			firstrow = Integer.getInteger(hashMap.getMapData("firstrow"));
			maxrow = Integer.getInteger(hashMap.getMapData("maxrow"));
			if (firstrow != null) {
				query.setFirstResult(firstrow);
			}
			if (maxrow != null) {
				query.setMaxResults(maxrow);
			}
			List<NewsClientsoftupdaterule> queryList=new ArrayList<NewsClientsoftupdaterule>();
			queryList = query.getResultList();
			for(NewsClientsoftupdaterule newsclientsur_temp:queryList){
				ClientUpgradePojo clientUpgradePojo_temp = new ClientUpgradePojo();
				clientUpgradePojo_temp.setClientua(newsclientsur_temp.getNewsClientsoftupdaterulePK().getClientua());
				clientUpgradePojo_temp.setClientchannel(newsclientsur_temp.getNewsClientsoftupdaterulePK().getClientchannel());
				clientUpgradePojo_temp.setClientsn(newsclientsur_temp.getNewsClientsoftupdaterulePK().getClientsn());
				clientUpgradePojo_temp.setIsmust(newsclientsur_temp.getIsmust());
				clientUpgradePojo_temp.setUpdateurl(newsclientsur_temp.getUpdateurl());
				clientUpgradePojo_temp.setRuleupdatetime(newsclientsur_temp.getRuleupdatetime());
				clientUpgradePojo_temp.setUpdatetip(newsclientsur_temp.getUpdatetip());
				list.add(clientUpgradePojo_temp);
			}
			logger.info("hql:"+hql);
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 检查是否已存在：客户端UA和渠道都相同时，已存在。
	 * @param genrename
	 * @return
	 */
	public boolean checkClientUpgradePojo(ClientUpgradePojo ncu) {
		String sql = "SELECT clientua FROM news_clientsoftupdaterule where 1=1 ";
		if (ncu.getClientua() != null && !"".equals(ncu.getClientua())) {
			sql += " and clientua = '" + ncu.getClientua() + "'";
		}
		if (ncu.getClientchannel() != null && !"".equals(ncu.getClientchannel())) {
			sql += " and clientchannel = '" + ncu.getClientchannel() + "'";
		}
		if (ncu.getClientsn() != null && !"".equals(ncu.getClientsn())) {
			sql += " and clientsn = '" + ncu.getClientsn() + "'";
		}
		logger.info("sql:"+sql);
		try {
			return this.queryExist(sql);
		} catch (SQLException e) {
			log.info("checkClientUpgradePojo fail !!!" + e.getMessage());
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean addClientupgradeinfo(ClientUpgradePojo ncu){
		try {
			
			this.sql="insert into news_clientsoftupdaterule (clientua,clientchannel,clientsn,updateurl,ismust,ruleupdatetime,upgrademsg,updatetip) values(?,?,?,?,?,now(),?,?)";
			  Query	query=em.createNativeQuery(sql);
			
			   
			   query.setParameter(1, ncu.getClientua());
			   query.setParameter(2, ncu.getClientchannel());
			   query.setParameter(3, ncu.getClientsn());
			   query.setParameter(4, ncu.getUpdateurl());
			   query.setParameter(5, ncu.getIsmust());
			   query.setParameter(6, ncu.getUpgradeMsg());
			   query.setParameter(7, ncu.getUpdatetip());
			   query.executeUpdate();
			return true;
		} catch (Exception e) {
			logger.error("addClientUpgradeinfo fail !!!" + e.getMessage());
			e.printStackTrace();
		} 
		return false;
	}
	
	public Boolean updateClientupgradeinfo(ClientUpgradePojo ncu,String clientua_old,String clientchannel_old,String clientsn_old){
		Boolean resultb = true;
		try{
			String jpql = "UPDATE NewsClientsoftupdaterule ncu SET ncu.newsClientsoftupdaterulePK.clientua=:clientua,ncu.newsClientsoftupdaterulePK.clientchannel=:clientchannel,ncu.newsClientsoftupdaterulePK.clientsn=:clientsn," +
			"ncu.updateurl=:updateurl,ncu.ismust=:ismust,ncu.upgradeMsg=:upgrademsg,ncu.updatetip=:updatetip";
			if(ncu.getRuleupdatetime() != null && !"".equals(ncu.getRuleupdatetime())){
				jpql +=",ncu.ruleupdatetime=:ruleupdatetime";
			}
			jpql += " WHERE ncu.newsClientsoftupdaterulePK.clientua=:clientua_old AND ncu.newsClientsoftupdaterulePK.clientchannel=:clientchannel_old AND ncu.newsClientsoftupdaterulePK.clientsn=:clientsn_old";
			Query query = em.createQuery(jpql);			
			query.setParameter("clientua", ncu.getClientua());
			query.setParameter("clientchannel", ncu.getClientchannel());
			query.setParameter("clientsn", ncu.getClientsn());
			query.setParameter("updateurl", ncu.getUpdateurl());
			query.setParameter("ismust",ncu.getIsmust());
			query.setParameter("upgrademsg",ncu.getUpgradeMsg());
			query.setParameter("updatetip",ncu.getUpdatetip());
			query.setParameter("clientua_old", clientua_old);
			query.setParameter("clientchannel_old", clientchannel_old);
			query.setParameter("clientsn_old", clientsn_old);
			if(ncu.getRuleupdatetime() != null && !"".equals(ncu.getRuleupdatetime())){
				query.setParameter("ruleupdatetime",ncu.getRuleupdatetime());
			}
			logger.info("jpql:"+jpql);
			int iCode = query.executeUpdate();
			if(iCode < 0){
				resultb = false;
			}
		}catch(Exception e){
			logger.warn(e.getMessage());
			resultb = false;
		}
		return resultb;
	}
	
	public int getClientUpgradeCount(ClientUpgradePojo ncu) {
		String hql = "SELECT count(ncu.newsClientsoftupdaterulePK.clientua) FROM NewsClientsoftupdaterule ncu where 1=1";
		if (ncu !=null){
			if (ncu.getClientua() != null && !"".equals(ncu.getClientua())) {
				hql += " and ncu.newsClientsoftupdaterulePK.clientua like '%" + ncu.getClientua() + "%'";
			}
			if (ncu.getClientchannel() != null && !"".equals(ncu.getClientchannel())) {
				hql += " and ncu.newsClientsoftupdaterulePK.clientchannel like '%" + ncu.getClientchannel() + "%'";
			}
			if (ncu.getClientsn() != null && !"".equals(ncu.getClientsn())) {
				hql += " and ncu.newsClientsoftupdaterulePK.clientsn like '%" + ncu.getClientsn() + "%'";
			}
		}
		hql += " order by ncu.ruleupdatetime desc";
		logger.info("hql:"+hql);
		Object obj = getSingleResult(hql);
		return obj==null ? 0 : Integer.parseInt(obj.toString());
	}

	//删除
	public boolean delClientUpgrade(String clientua, String channel,String clientsn) {
		String sql = "DELETE FROM news_clientsoftupdaterule WHERE clientua = '"+clientua+"' AND clientchannel = '"+channel+"' AND clientsn = '"+clientsn+"'";
		logger.info("sql:"+sql);
		try {
			return execUpdate(sql);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
}
